import { NDynamicInput } from "naive-ui";
import type {
  DynamicInputProps,
  DynamicInputDefaultSlotProps,
  DynamicInputActionSlotProps,
} from "naive-ui";

import type { BaseComponentType, OmitHTMLAttributes } from "../types";
import { getSlotsDom } from "../utils";
export type DynamicInputFactoryConstructorType = {
  props?: DynamicInputProps;
  attrs?: OmitHTMLAttributes<"onInput">;
  defaultSlot?: (v?: DynamicInputDefaultSlotProps) => BaseComponentType;
  actionSlot?: (v?: DynamicInputActionSlotProps) => BaseComponentType;
  createButtonDefault?: BaseComponentType;
  createButtonIcon?: BaseComponentType;
};
export class DynamicInputFactory {
  private props: DynamicInputProps = {};
  private attrs: OmitHTMLAttributes<"onInput"> = {};
  private defaultSlot: (v?: DynamicInputDefaultSlotProps) => BaseComponentType =
    () => null;
  private actionSlot: (v?: DynamicInputActionSlotProps) => BaseComponentType =
    () => null;
  private createButtonDefaultSlot: BaseComponentType = null;
  private createButtonIconSlot: BaseComponentType = null;

  constructor(data?: DynamicInputFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.actionSlot) this.setAction(data.actionSlot);
    if (data?.createButtonDefault)
      this.setCreateButtonDefault(data.createButtonDefault);
    if (data?.createButtonIcon) this.setCreateButtonIcon(data.createButtonIcon);
  }
  setAttrs(attrs: OmitHTMLAttributes<"onInput">) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: DynamicInputProps) {
    this.props = props;
    return this;
  }
  setDefault(
    component: (v?: DynamicInputDefaultSlotProps) => BaseComponentType
  ) {
    this.defaultSlot = component;
    return this;
  }

  setAction(component: (v?: DynamicInputActionSlotProps) => BaseComponentType) {
    this.actionSlot = component;
    return this;
  }
  setCreateButtonDefault(component: BaseComponentType) {
    this.createButtonDefaultSlot = component;
    return this;
  }
  setCreateButtonIcon(component: BaseComponentType) {
    this.createButtonIconSlot = component;
    return this;
  }
  create() {
    return (
      <NDynamicInput {...this.props} {...this.attrs}>
        {{
          default:
            this.defaultSlot() != null
              ? (v: DynamicInputDefaultSlotProps) =>
                  getSlotsDom(this.defaultSlot(v))
              : null,
          action: (v: DynamicInputActionSlotProps) =>
            getSlotsDom(this.actionSlot(v)),
          "create-button-default": () =>
            getSlotsDom(this.createButtonDefaultSlot),
          "create-button-icon": () => getSlotsDom(this.createButtonIconSlot),
        }}
      </NDynamicInput>
    );
  }
}
